<?php
namespace api\modules\shop\controllers;

use common\models\Code;
use common\models\Shop;
use common\service\ConstantService;
use common\service\SmsService;
use common\service\WechatService;

class ShopController extends ShopBaseController
{
    /**
     * 自动登录
     */
    public function actionAutoLogin(){
        if($this->isGet()){
            $code = $this->get('code');
            $appid = \Yii::$app->params['shop_wechat']['appid'];
            $secret = \Yii::$app->params['shop_wechat']['secret'];
            $request = WechatService::getWechatOpenid($code,$appid,$secret);
            $request = json_decode($request,true);
            if(isset($request['errcode'])){
                return $this->renderJson([],'code无效',ConstantService::$response_fail);
            }
            $openid = $request['openid'];
            $shop = Shop::find()->where(['openid'=>$openid])->asArray()->one();
            //最终都返回Token
            $token = base64_encode(base64_encode($openid).'-'.$this->salt);
            if(!$shop){
                return $this->renderJson($token,'需要绑定',ConstantService::$response_fail_auth);
            }
//            if(empty($user['nickname']) && empty($user['avatarurl'])){
//                return $this->renderJson([],'该用户未授权',ConstantService::$response_fail_auth);
//            }
            return $this->renderJson($token,'操作成功',ConstantService::$response_success);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);


    }

    /**
     * 管理员绑定
     */
    public function actionSaveShopInfo(){
        if($this->isPost()){
            $token = isset(\Yii::$app->request->headers['Auth']) ? \Yii::$app->request->headers['Auth']:'';
            $token = explode('-',base64_decode($token));
            $openid = base64_decode($token[0]);
            $phone = $this->post('phone');
            $code = $this->post('code');
            //判断验证码是否正确
            $msg = $this->_checkcode($phone,$code);
            if($msg !='ok'){
                return $this->renderJson([],$msg,ConstantService::$response_fail);
            }

            $shop_model = Shop::find()->where(['admin_phone'=>$phone])->one();
            if(!$shop_model){
                return $this->renderJson([],'该账号不存在',ConstantService::$response_fail);
            }
            if($shop_model['status'] != 1){
                return $this->renderJson([],'该账号不存在状态不正确,请联系管理员',ConstantService::$response_fail);
            }

            $shop_model->openid = $openid;
            if($shop_model->save()){
                return $this->renderJson([],'绑定成功',ConstantService::$response_success);
            }
            return $this->renderJson([],'操作失误,请重试',ConstantService::$response_fail);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);
    }

    /**
     * 发送短信验证码
     */
    public function actionSendCode(){
        if($this->isPost()){
            $phone = $this->post('phone');
            if(!preg_match('/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|17[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$/',$phone))
            {
                return $this->renderJson([],'请输入正确的管理员手机号',ConstantService::$response_fail);
            }
            if(!$phone){
                return $this->renderJson([],'参数错误',ConstantService::$response_fail);
            }
            $data = Code::find()->where(['phone'=>$phone])->orderBy(['id'=>SORT_DESC])->one();
            if((time()-$data['time'])>300){
                $code = SmsService::addSms($phone);
                //调用发送短信方法
                if($code){
                    return $this->renderJson([],'发送成功',ConstantService::$response_success);
                }else{
                    return $this->renderJson([],'系统错误稍后再试',ConstantService::$response_fail);
                }
            }else{
                return $this->renderJson([],'请5分钟后再试一试',ConstantService::$response_fail);
            }
        }
        return $this->renderJson([],'接口错误',ConstantService::$response_fail);
    }


    /**
     * 验证验证码是否正确
     * @param $phone
     * @param $code
     * @return string
     */
    private function _checkcode($phone,$code){
        if(empty($phone)){
            return '管理员手机号不能为空!';
        }
        if( empty($code)){
            return '验证码不能为空!';
        }
        $data = Code::find()->where(['phone'=>$phone])->orderBy(['id'=>SORT_DESC])->asArray()->one();
        if((time()-$data['time'])>300 || $data['is_del'] == 1){
            return '验证码错误';
        }
        if($data['code'] == $code){
            $Codes = Code::findOne(['id'=>$data['id']]);
            $Codes->is_del = 1;
            $Codes->save();
            return 'ok';
        }else{
            return '验证码错误';
        }
    }


}